﻿        using System;   
using System.Collections.Generic;
        using System.Management;
        using System.Windows.Forms;   
using System.Drawing.Printing;  
namespace OpenSoftStore.Client.Common
{
    public class Printer : Form
    {

        public Printer()
        {
            this.Text = "本地打印机列表";
            ListBox fListBox = new ListBox();
            fListBox.Dock = DockStyle.Fill;
            foreach (String fPrinterName in LocalPrinter.GetLocalPrinters())
                fListBox.Items.Add(fPrinterName);
            this.Controls.Add(fListBox);

            //Printer p = new Printer();
            //p.Show();
            //MessageBox.Show(LocalPrinter.GetDefaultPrinter());
            LocalPrinter.SetDefaultPrinter("Microsoft XPS Document Writer");
            LocalPrinter.fPrintDocument.Print();
   
        }
    }

    /// </summary>    
        public class LocalPrinter
        {
            public static PrintDocument fPrintDocument = new PrintDocument();

            /// <summary>    
            /// 获取本机默认打印机名称    
            /// </summary>    
            public static String DefaultPrinter
            {
                get { return fPrintDocument.PrinterSettings.PrinterName; }
            }

            /// <summary>    
            /// 获取本机的打印机列表。列表中的第一项就是默认打印机。    
            /// </summary>    
            public static List<String> GetLocalPrinters()
            {
                List<String> fPrinters = new List<string>();
                fPrinters.Add(DefaultPrinter); // 默认打印机始终出现在列表的第一项    
                foreach (String fPrinterName in PrinterSettings.InstalledPrinters)
                {
                    if (!fPrinters.Contains(fPrinterName))
                        fPrinters.Add(fPrinterName);
                }
                return fPrinters;
            }

            /// <summary>
            /// 获取本地当前默认打印机
            /// </summary>
            /// <returns></returns>
            public static string GetDefaultPrinter()
            {
                ManagementObjectSearcher searcher = new
                    ManagementObjectSearcher("select * from Win32_Printer");

                string DefaultPrinter = "";
                foreach (ManagementObject Printer in searcher.Get())
                {
                    if (bool.Parse(Printer["Default"].ToString()))
                    {
                        DefaultPrinter = Printer["Name"].ToString();
                        break;
                    }
                }
                return DefaultPrinter;
            }

            /// <summary>
            /// 设置本地当前默认打印机
            /// </summary>
            /// <returns></returns>
            public static void SetDefaultPrinter(string PrinterName)
            {
                ManagementObjectSearcher searcher = new
                    ManagementObjectSearcher("select * from Win32_Printer");

                foreach (ManagementObject Printer in searcher.Get())
                {
                    if (string.Compare(Printer["Name"].ToString(), PrinterName, true) == 0)
                    {
                        Printer.InvokeMethod("SetDefaultPrinter", null);
                        break;
                    }
                }

            }
        }
}



